package api

import (
	"net/http"

	"example.com/rest/example/dao"
	"example.com/rest/example/model"

	"github.com/gin-gonic/gin"
	"github.com/guregu/null"
	"github.com/julienschmidt/httprouter"
)

var (
	_ = null.Bool{}
)

func configPlaylistsRouter(router *httprouter.Router) {
	router.GET("/playlists", GetAllPlaylists)
	router.POST("/playlists", AddPlaylists)
	router.GET("/playlists/:argPlaylistID", GetPlaylists)
	router.PUT("/playlists/:argPlaylistID", UpdatePlaylists)
	router.DELETE("/playlists/:argPlaylistID", DeletePlaylists)
}

func configGinPlaylistsRouter(router gin.IRoutes) {
	router.GET("/playlists", ConverHttprouterToGin(GetAllPlaylists))
	router.POST("/playlists", ConverHttprouterToGin(AddPlaylists))
	router.GET("/playlists/:argPlaylistID", ConverHttprouterToGin(GetPlaylists))
	router.PUT("/playlists/:argPlaylistID", ConverHttprouterToGin(UpdatePlaylists))
	router.DELETE("/playlists/:argPlaylistID", ConverHttprouterToGin(DeletePlaylists))
}

// GetAllPlaylists is a function to get a slice of record(s) from playlists table in the main database
// @Summary Get list of Playlists
// @Tags Playlists
// @Description GetAllPlaylists is a handler to get a slice of record(s) from playlists table in the main database
// @Accept  json
// @Produce  json
// @Param   page     query    int     false        "page requested (defaults to 0)"
// @Param   pagesize query    int     false        "number of records in a page  (defaults to 20)"
// @Param   order    query    string  false        "db sort order column"
// @Success 200 {object} api.PagedResults{data=[]model.Playlists}
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /playlists [get]
// http "http://localhost:8080/playlists?page=0&pagesize=20" X-Api-User:user123
func GetAllPlaylists(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)
	page, err := readInt(r, "page", 0)
	if err != nil || page < 0 {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	pagesize, err := readInt(r, "pagesize", 20)
	if err != nil || pagesize <= 0 {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	order := r.FormValue("order")

	if err := ValidateRequest(ctx, r, "playlists", model.RetrieveMany); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	records, totalRows, err := dao.GetAllPlaylists(ctx, page, pagesize, order)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	result := &PagedResults{Page: page, PageSize: pagesize, Data: records, TotalRecords: totalRows}
	writeJSON(ctx, w, result)
}

// GetPlaylists is a function to get a single record from the playlists table in the main database
// @Summary Get record from table Playlists by  argPlaylistID
// @Tags Playlists
// @ID argPlaylistID
// @Description GetPlaylists is a function to get a single record from the playlists table in the main database
// @Accept  json
// @Produce  json
// @Param  argPlaylistID path int true "PlaylistId"
// @Success 200 {object} model.Playlists
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError "ErrNotFound, db record for id not found - returns NotFound HTTP 404 not found error"
// @Router /playlists/{argPlaylistID} [get]
// http "http://localhost:8080/playlists/1" X-Api-User:user123
func GetPlaylists(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argPlaylistID, err := parseInt32(ps, "argPlaylistID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	if err := ValidateRequest(ctx, r, "playlists", model.RetrieveOne); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	record, err := dao.GetPlaylists(ctx, argPlaylistID)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, record)
}

// AddPlaylists add to add a single record to playlists table in the main database
// @Summary Add an record to playlists table
// @Description add to add a single record to playlists table in the main database
// @Tags Playlists
// @Accept  json
// @Produce  json
// @Param Playlists body model.Playlists true "Add Playlists"
// @Success 200 {object} model.Playlists
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /playlists [post]
// echo '{"playlist_id": 42,"name": "srypLkMmKSZJpxLNEjdsZRtCq"}' | http POST "http://localhost:8080/playlists" X-Api-User:user123
func AddPlaylists(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)
	playlists := &model.Playlists{}

	if err := readJSON(r, playlists); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := playlists.BeforeSave(); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
	}

	playlists.Prepare()

	if err := playlists.Validate(model.Create); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := ValidateRequest(ctx, r, "playlists", model.Create); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	var err error
	playlists, _, err = dao.AddPlaylists(ctx, playlists)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, playlists)
}

// UpdatePlaylists Update a single record from playlists table in the main database
// @Summary Update an record in table playlists
// @Description Update a single record from playlists table in the main database
// @Tags Playlists
// @Accept  json
// @Produce  json
// @Param  argPlaylistID path int true "PlaylistId"
// @Param  Playlists body model.Playlists true "Update Playlists record"
// @Success 200 {object} model.Playlists
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /playlists/{argPlaylistID} [put]
// echo '{"playlist_id": 42,"name": "srypLkMmKSZJpxLNEjdsZRtCq"}' | http PUT "http://localhost:8080/playlists/1"  X-Api-User:user123
func UpdatePlaylists(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argPlaylistID, err := parseInt32(ps, "argPlaylistID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	playlists := &model.Playlists{}
	if err := readJSON(r, playlists); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := playlists.BeforeSave(); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
	}

	playlists.Prepare()

	if err := playlists.Validate(model.Update); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := ValidateRequest(ctx, r, "playlists", model.Update); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	playlists, _, err = dao.UpdatePlaylists(ctx,
		argPlaylistID,
		playlists)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, playlists)
}

// DeletePlaylists Delete a single record from playlists table in the main database
// @Summary Delete a record from playlists
// @Description Delete a single record from playlists table in the main database
// @Tags Playlists
// @Accept  json
// @Produce  json
// @Param  argPlaylistID path int true "PlaylistId"
// @Success 204 {object} model.Playlists
// @Failure 400 {object} api.HTTPError
// @Failure 500 {object} api.HTTPError
// @Router /playlists/{argPlaylistID} [delete]
// http DELETE "http://localhost:8080/playlists/1" X-Api-User:user123
func DeletePlaylists(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argPlaylistID, err := parseInt32(ps, "argPlaylistID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	if err := ValidateRequest(ctx, r, "playlists", model.Delete); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	rowsAffected, err := dao.DeletePlaylists(ctx, argPlaylistID)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeRowsAffected(w, rowsAffected)
}
